/*
 *      Graphics.cpp
 *      
 *      Copyright 2008 William Hamrick <brick@brick-laptop>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */


//#include <allegro.h>

terrain redrawmap(terrain map1)
{
	int i;
	for (int y = 0 ; y < map1.tileheight ; y++)
	{
		for (int x = 0 ; x < map1.tilewidth ; x++)
		{
			i = y * map1.tileheight + x;
			draw_sprite(map1.map, map1.tiles.at(i).background, x * map1.size, y * map1.size);
			draw_sprite(map1.map, map1.tiles.at(i).forground, x * map1.size, y * map1.size);
		}
	}
	return map1;	
}

terrain redrawtile(terrain map1, int x, int y)
{
	int i = y * map1.tileheight + x;
	draw_sprite(map1.map, map1.tiles.at(i).background, x * map1.size, y * map1.size);
	draw_sprite(map1.map, map1.tiles.at(i).forground, x * map1.size, y * map1.size);
	return map1;
}

terrain makemap1(terrain map1)
{
	int i;
	tile i1;
	map1.size = 32;
	map1.height = 320;
	map1.width = 320;
	map1.camera[0] = 0;
	map1.camera[1] = 0;
	map1.tileheight = 15;
	map1.tilewidth = 15;
	set_gfx_mode (GFX_AUTODETECT_WINDOWED, map1.height, map1.width, 0, 0);
	set_color_depth(32);
	map1.tiles.assign(map1.tileheight * map1.tilewidth, i1);
	map1.map = create_bitmap(map1.tilewidth * map1.size, map1.tileheight * map1.size);
	clear_to_color(map1.map, makecol(0,255,0));
	for (int y = 0 ; y < map1.tileheight ; y++)
	{
		for (int x = 0 ; x < map1.tilewidth ; x++)
		{
			i = y * map1.tileheight + x;
			map1.tiles.at(i).background = create_bitmap(map1.size, map1.size);
			clear_to_color(map1.tiles.at(i).background, makecol(0, x * 255 / map1.tilewidth, y * 255 / map1.tileheight));
			//rect(map1.tiles.at(i).background, 0, 0, map1.size - 1, map1.size - 1, makecol(0, 0, 0));
			map1.tiles.at(i).forground = create_bitmap(map1.size, map1.size);
			clear_to_color(map1.tiles.at(i).forground, bitmap_mask_color(map1.map));
			map1.tiles.at(i).walkable = true;
			map1.tiles.at(i).hasunit = 0;
//			map1.tiles.at(i).lup, map1.tiles.at(i).ldown, map1.tiles.at(i).lleft, map1.tiles.at(i).lright, map1.tiles.at(i).eup, map1.tiles.at(i).edown, map1.tiles.at(i).eleft, map1.tiles.at(i).eright = true;
		}
	}
	map1 = redrawmap(map1);
	map1.buffer = create_bitmap(map1.height, map1.width);
	return map1;
}

terrain drawmapobjects(terrain map1, vector<mapobject> allmapobjects)
{
	int center[2];
	center[0] = allmapobjects.at(0).location[0] + (map1.width - allmapobjects.at(0).size[0]) / 2;
	center[1] = allmapobjects.at(0).location[1] + (map1.height - allmapobjects.at(0).size[1]) / 2;
	
	blit(allmapobjects.at(0).pic, map1.buffer, 0, 0, (map1.width - allmapobjects.at(0).size[0]) / 2, (map1.height - allmapobjects.at(0).size[1]) / 2, allmapobjects.at(0).size[0], allmapobjects.at(0).size[1]);
	
	for (unsigned int i = 1 ; i < allmapobjects.size() ; i++)
	{
		blit(allmapobjects.at(i).pic, map1.buffer, 0, 0, allmapobjects.at(i).location[0] + center[0], allmapobjects.at(i).location[1] + center[1], allmapobjects.at(i).size[0], allmapobjects.at(i).size[1]);
	}
	return map1;
}
